﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Xml.Serialization;
using System.Drawing;
using System.Xml;

namespace NursingFormSystem
{
    /// <summary>
    /// 体温单设置
    /// </summary>
    [System.Runtime.InteropServices.ComVisible(false)]
    public class TemperatureDocumentOptions
    {
        //Brush b= new SolidBrush(ColorTranslator.FromHtml(HospitalNode.Attributes["Color"].Value)),
        public XmlDocument configXml = new XmlDocument();
        /// <summary>
        /// 初始化对象
        /// </summary>
        public TemperatureDocumentOptions()
        {
            //dynamic dx = new DynamicXml(xmlFilePath);
            //foreach (dynamic dy in dx)
            //{
            //    string s = dy.Attributes["Name"].value;
            //}
        }

        private float _Left = 0f;
        /// <summary>
        /// 左端位置
        /// </summary>
        [DefaultValue(0f)]
        [XmlIgnore]
        public float Left
        {
            get { return _Left; }
            set { _Left = value; }
        }

        private float _Top = 0f;
        /// <summary>
        /// 顶端位置
        /// </summary>
        [DefaultValue(0)]
        [XmlIgnore]
        public float Top
        {
            get { return _Top; }
            set { _Top = value; }
        }

        private float _Width = 750;
        /// <summary>
        /// 宽度
        /// </summary>
        [DefaultValue(750)]
        [XmlIgnore]
        public float Width
        {
            get { return _Width; }
            set { _Width = value; }
        }

        private float _Height = 1020;
        /// <summary>
        /// 高度
        /// </summary>
        [DefaultValue(1020)]
        [XmlIgnore]
        public float Height
        {
            get { return _Height; }
            set { _Height = value; }
        }

        /// <summary>
        /// 边界
        /// </summary>
        [Browsable(false)]
        [System.Xml.Serialization.XmlIgnore]
        public RectangleF Bounds
        {
            get
            {
                return new RectangleF(
                   this._Left,
                   this._Top,
                   this._Width,
                   this._Height);
            }
            set
            {
                this._Left = value.Left;
                this._Top = value.Top;
                this._Width = value.Width;
                this._Height = value.Height;
            }
        }

        private float _TopTemp = 39;
        /// <summary>
        /// 降温连线临界点
        /// </summary>
        [DefaultValue(39)]
        [XmlIgnore]
        public float TopTemp
        {
            get { return _TopTemp; }
            set { _TopTemp = value; }
        }

        private float _Space = 39;
        /// <summary>
        /// 连线是否有空隙
        /// </summary>
        [DefaultValue(0)]
        [XmlIgnore]
        public float Space
        {
            get { return _Space; }
            set { _Space = value; }
        }

        private string _Code = "";
        /// <summary>
        /// xml Code
        /// </summary>
        [DefaultValue("")]
        public string Code
        {
            get
            {
                return _Code;
            }
            set
            {
                _Code = value;
            }
        }

        private string _Line = "0";
        /// <summary>
        /// 是否显示连线
        /// </summary>
        [DefaultValue("0")]
        public string Line
        {
            get
            {
                return _Line;
            }
            set
            {
                _Line = value;
            }
        }

        private string _DropedBeat = null;
        /// <summary>
        /// 脉搏短绌是否显示 1-是
        /// </summary>
        [DefaultValue("0")]
        public string DropedBeat
        {
            get { return _DropedBeat; }
            set { _DropedBeat = value; }
        }

        private string _Respirator = null;
        /// <summary>
        /// 用呼吸机是否显示 1-是
        /// </summary>
        [DefaultValue("0")]
        public string Respirator
        {
            get { return _Respirator; }
            set { _Respirator = value; }
        }

        private string _CustomBreath = "0";
        /// <summary>
        /// 是否显示显示底部呼吸位置
        /// </summary>
        [DefaultValue("0")]
        public string CustomBreath
        {
            get
            {
                return _CustomBreath;
            }
            set
            {
                _CustomBreath = value;
            }
        }

        private string _ArrowColor = "";
        /// <summary>
        /// 小于35度下箭头颜色
        /// </summary>
        [DefaultValue("")]
        public string ArrowColor
        {
            get
            {
                return _ArrowColor;
            }
            set
            {
                _ArrowColor = value;
            }
        }

        private string _Remark = "";
        /// <summary>
        /// 特殊说明
        /// </summary>
        [DefaultValue("")]
        public string Remark
        {
            get
            {
                return _Remark;
            }
            set
            {
                _Remark = value;
            }
        }

        private string[] _HourTicks = new string[] { "3:00", "7:00", "11:00", "15:00", "19:00", "23:00" };
        /// <summary>
        /// 显示的时刻数
        /// </summary>
        public string[] HourTicks
        {
            get
            {
                return _HourTicks;
            }
            set
            {
                _HourTicks = value;
            }
        }

        private string[] _HourTickColors = new string[] { "#FF0000", "#FF0000", "#000000", "#000000", "#000000", "#FF0000" };
        /// <summary>
        /// 显示的时刻颜色
        /// </summary>
        public string[] HourTickColors
        {
            get
            {
                return _HourTickColors;
            }
            set
            {
                _HourTickColors = value;
            }
        }

        private string[] _AllAddHourTicks = new string[] { "0","0:30","1","1:30","2","2:30","3:30","4","4:30","5","5:30","6",
            "6:30","7:30","8","8:30","9","9:30","10","10:30","11:30","12","12:30","13","13:30","14","14:30","15:30","16","16:30",
            "17","17:30","18","18:30","19:30","20","20:30","21","21:30","22","22:30","23:30"};
        /// <summary>
        /// 显示的时刻数
        /// </summary>
        public string[] AllAddHourTicks
        {
            get
            {
                return _AllAddHourTicks;
            }
            set
            {
                _AllAddHourTicks = value;
            }
        }

        private int _YSplitNum = 8;
        /// <summary>
        /// Y轴分割区域数量
        /// </summary>
        [DefaultValue(8)]
        public int YSplitNum
        {
            get
            {
                return _YSplitNum;
            }
            set
            {
                _YSplitNum = value;
            }
        }

        private int _YSplitLittleNum = 0;
        /// <summary>
        /// 顶部Y轴预留小格数
        /// </summary>
        [DefaultValue(0)]
        public int YSplitLittleNum
        {
            get
            {
                return _YSplitLittleNum;
            }
            set
            {
                _YSplitLittleNum = value;
            }
        }

        private int _YSplitLittleNums = 5;
        /// <summary>
        /// Y轴一大格分多少小格
        /// </summary>
        [DefaultValue(5)]
        public int YSplitLittleNums
        {
            get
            {
                return _YSplitLittleNums;
            }
            set
            {
                _YSplitLittleNums = value;
            }
        }

        private float _SymbolSize = 10;
        /// <summary>
        /// 数据点符号大小
        /// </summary>
        [DefaultValue(10)]
        public float SymbolSize
        {
            get
            {
                return _SymbolSize;
            }
            set
            {
                _SymbolSize = value;
            }
        }

        private string _FontName = "宋体";
        /// <summary>
        /// 字体名称
        /// </summary>
        [DefaultValue("宋体")]
        public string FontName
        {
            get
            {
                return _FontName;
            }
            set
            {
                _FontName = value;
            }
        }

        private float _FontSize = 9f;
        /// <summary>
        /// 字体的大小
        /// </summary>
        [DefaultValue(9f)]
        public float FontSize
        {
            get
            {
                return _FontSize;
            }
            set
            {
                _FontSize = value;
            }
        }

        private List<TitleLineInfo> _HeaderLines = new List<TitleLineInfo>();
        /// <summary>
        /// 标题行信息
        /// </summary>
        [XmlArrayItem("Line", typeof(TitleLineInfo))]
        public List<TitleLineInfo> HeaderLines
        {
            get
            {
                return _HeaderLines;
            }
            set
            {
                _HeaderLines = value;
            }
        }

        private List<TitleLineInfo> _FooterLines = new List<TitleLineInfo>();
        /// <summary>
        /// 页脚行信息
        /// </summary>
        [XmlArrayItem("Line", typeof(TitleLineInfo))]
        public List<TitleLineInfo> FooterLines
        {
            get
            {
                return _FooterLines;
            }
            set
            {
                _FooterLines = value;
            }
        }

        private List<FooterRemark> _FooterRemarks = new List<FooterRemark>();
        /// <summary>
        /// 页脚备注信息
        /// </summary>
        [XmlArrayItem("Line", typeof(FooterRemark))]
        public List<FooterRemark> FooterRemarks
        {
            get
            {
                return _FooterRemarks;
            }
            set
            {
                _FooterRemarks = value;
            }
        }

        private YAxisInfoList _YAxisInfos = new YAxisInfoList();
        /// <summary>
        /// Y坐标轴信息列表
        /// </summary>
        [XmlArrayItem("YAxis", typeof(YAxisInfo))]
        public YAxisInfoList YAxisInfos
        {
            get
            {
                return _YAxisInfos;
            }
            set
            {
                _YAxisInfos = value;
            }
        }


        private Color _BackColor = Color.Transparent;
        /// <summary>
        /// 背景色
        /// </summary>
        [DefaultValue(typeof(Color), "Transparent")]
        [XmlIgnore]
        public Color BackColor
        {
            get
            {
                return _BackColor;
            }
            set
            {
                _BackColor = value;
            }
        }

        private string _BeginFormatString = "yyyy-MM-dd";
        /// <summary>
        /// 输出日期数据使用的格式化字符串
        /// </summary>
        [DefaultValue("yyyy-MM-dd")]
        public string BeginFormatString
        {
            get
            {
                return _BeginFormatString;
            }
            set
            {
                _BeginFormatString = value;
            }
        }

        private string _DateFormatString = "yyyy-MM-dd";
        /// <summary>
        /// 输出日期数据使用的格式化字符串
        /// </summary>
        [DefaultValue("yyyy-MM-dd")]
        public string DateFormatString
        {
            get
            {
                return _DateFormatString;
            }
            set
            {
                _DateFormatString = value;
            }
        }

        private int _InDateBeginDay = 1;
        /// <summary>
        /// 住院天数展示项从0开始还是从1开始
        /// </summary>
        [DefaultValue(1)]
        public int InDateBeginDay
        {
            get
            {
                return _InDateBeginDay;
            }
            set
            {
                _InDateBeginDay = value;
            }
        }

        private int _DateBeginDay = 1;
        /// <summary>
        /// 天数展示项从0开始还是从1开始
        /// </summary>
        [DefaultValue(1)]
        public int DateBeginDay
        {
            get
            {
                return _DateBeginDay;
            }
            set
            {
                _DateBeginDay = value;
            }
        }

        private int _DateOverDay = 15;
        /// <summary>
        /// 手术/产后多少天后的数值不显示
        /// </summary>
        [DefaultValue(15)]
        public int DateOverDay
        {
            get
            {
                return _DateOverDay;
            }
            set
            {
                _DateOverDay = value;
            }
        }

        private string _InShowType = "/";
        /// <summary>
        /// 手术/产后天数分隔格式
        /// </summary>
        [DefaultValue("/")]
        public string InShowType
        {
            get
            {
                return _InShowType;
            }
            set
            {
                _InShowType = value;
            }
        }

        private int _NumOfDaysInOnePage = 7;
        /// <summary>
        /// 每页显示的天数
        /// </summary>
        [DefaultValue(7)]
        public int NumOfDaysInOnePage
        {
            get
            {
                return _NumOfDaysInOnePage;
            }
            set
            {
                _NumOfDaysInOnePage = value;
            }
        }

        private string _MemoTimeShow = "";
        /// <summary>
        /// 哪些事件的时间显示
        /// </summary>
        [DefaultValue("")]
        public string MemoTimeShow
        {
            get
            {
                return _MemoTimeShow;
            }
            set
            {
                _MemoTimeShow = value;
            }
        }


        private string _IsEditMemoTime = "0";
        /// <summary>
        /// 事件的时间是否可编辑 1表示可以编辑 0表示否
        /// </summary>
        [DefaultValue("")]
        public string IsEditMemoTime
        {
            get
            {
                return _IsEditMemoTime;
            }
            set
            {
                _IsEditMemoTime = value;
            }
        }



        /// <summary>
        /// 设置为默认的设置
        /// </summary>
        public void InitDefaultSettings(string xmlContent)
        {
            if (!string.IsNullOrEmpty(xmlContent))
            {
                configXml.LoadXml(xmlContent);
            }
            else
            {
                string xmlFilePath = System.Windows.Forms.Application.StartupPath + @"\XML\config.xml";
                configXml.Load(xmlFilePath);
            }
            #region 取配置xml信息

            XmlNodeList CommonConfigList = configXml.SelectNodes("TemperatureDocument/CommonConfigs/CommonConfig");
            foreach (XmlNode CommonConfigNode in CommonConfigList)
            {
                switch (CommonConfigNode.Attributes["Id"].Value)
                {
                    case "YSplitNum":
                        YSplitNum = Convert.ToInt32(CommonConfigNode.Attributes["Value"].Value);
                        break;
                    case "YSplitLittleNum":
                        YSplitLittleNum = Convert.ToInt32(CommonConfigNode.Attributes["Value"].Value);
                        break;
                    case "YSplitLittleNums":
                        YSplitLittleNums = Convert.ToInt32(CommonConfigNode.Attributes["Value"].Value);
                        break;
                    case "HourTicks":
                        string[] strArray = CommonConfigNode.Attributes["Value"].Value.Split(new char[] { ',' });
                        HourTicks = strArray;//时刻
                        HourTickColors = CommonConfigNode.Attributes["Color"].Value.Split(new char[] { ',' });//颜色
                        break;
                    case "BeginFormatString":
                        BeginFormatString = CommonConfigNode.Attributes["Value"].Value;
                        break;
                    case "DateFormatString":
                        DateFormatString = CommonConfigNode.Attributes["Value"].Value;
                        break;
                    case "InDateBeginDay":
                        InDateBeginDay = Convert.ToInt32(CommonConfigNode.Attributes["Value"].Value);
                        break;
                    case "DateBeginDay":
                        DateBeginDay = Convert.ToInt32(CommonConfigNode.Attributes["Value"].Value);
                        break;
                    case "DateOverDay":
                        DateOverDay = Convert.ToInt32(CommonConfigNode.Attributes["Value"].Value);
                        break;
                    case "InShowType":
                        InShowType = CommonConfigNode.Attributes["Value"].Value;
                        break;
                    case "Code":
                        Code = CommonConfigNode.Attributes["Value"].Value;
                        break;
                    case "ArrowColor":
                        ArrowColor = CommonConfigNode.Attributes["Value"].Value;
                        break;
                    case "Remark":
                        Remark = CommonConfigNode.Attributes["Value"].Value;
                        break;
                    case "Line":
                        Line = CommonConfigNode.Attributes["Value"].Value;
                        break;
                    case "CustomBreath":
                        CustomBreath = CommonConfigNode.Attributes["Value"].Value;
                        break;
                    case "TopTemp":
                        TopTemp = Convert.ToSingle(CommonConfigNode.Attributes["Value"].Value);
                        break;
                    case "Space":
                        Space = Convert.ToSingle(CommonConfigNode.Attributes["Value"].Value);
                        break;
                    case "DropedBeat":
                        DropedBeat = CommonConfigNode.Attributes["Value"].Value;
                        break;
                    case "Respirator":
                        Respirator = CommonConfigNode.Attributes["Value"].Value;
                        break;
                    case "MemoTimeShow":
                        MemoTimeShow = CommonConfigNode.Attributes["Value"].Value;
                        break;
                    case "IsEditMemoTime":
                        IsEditMemoTime = CommonConfigNode.Attributes["Value"].Value;
                        break;
                    case "AllAddHourTicks":
                        strArray = CommonConfigNode.Attributes["Value"].Value.Split(',');
                        AllAddHourTicks = strArray;
                        break;
                    default:
                        break;
                }

            }    
            #endregion

            // 页眉行
            XmlNodeList HeaderLinesNodeList = configXml.SelectNodes("TemperatureDocument/HeaderLines/HeaderLine");
            foreach (XmlNode HeaderLinesNode in HeaderLinesNodeList)
            {
                switch (HeaderLinesNode.Attributes["Id"].Value)
                {
                    case "Days"://日期
                        this.HeaderLines.Add(new TitleLineInfo(
                            HeaderLinesNode.Attributes["Name"].Value,
                            TemperatureDocument.NullDate,
                            TitleLineValueType.SerialDate,
                            Convert.ToInt32(HeaderLinesNode.Attributes["Splits"].Value), 
                            HeaderLinesNode.Attributes["ShowType"] == null ? "" : HeaderLinesNode.Attributes["ShowType"].Value,
                            ""
                            ));
                        break;
                    case "HospitalDays"://住院日数
                        this.HeaderLines.Add(new TitleLineInfo(
                            HeaderLinesNode.Attributes["Name"].Value,
                            TemperatureDocument.NullDate,
                            TitleLineValueType.InDayIndex,
                            Convert.ToInt32(HeaderLinesNode.Attributes["Splits"].Value),
                            HeaderLinesNode.Attributes["ShowType"] == null ? "" : HeaderLinesNode.Attributes["ShowType"].Value,
                            ""
                            ));
                        break;
                    case "PostoperativeDays"://术后日数
                        this.HeaderLines.Add(new TitleLineInfo(
                            HeaderLinesNode.Attributes["Name"].Value,
                            TemperatureDocument.NullDate,
                            TitleLineValueType.DayIndex,
                            "手术/出生",
                            Convert.ToInt32(HeaderLinesNode.Attributes["Splits"].Value),
                            HeaderLinesNode.Attributes["ShowType"] == null ? "" : HeaderLinesNode.Attributes["ShowType"].Value,
                            ""
                            ));
                        break;
                    case "PostpartumDays"://产后日数
                        this.HeaderLines.Add(new TitleLineInfo(
                            HeaderLinesNode.Attributes["Name"].Value,
                            TemperatureDocument.NullDate,
                            TitleLineValueType.ChildBirthDay,
                            "分娩",
                            Convert.ToInt32(HeaderLinesNode.Attributes["Splits"].Value),
                            HeaderLinesNode.Attributes["ShowType"] == null ? "" : HeaderLinesNode.Attributes["ShowType"].Value,
                            ""
                            ));
                        break;
                    case "TimeInterval"://时间间隔
                        this.HeaderLines.Add(new TitleLineInfo(
                            HeaderLinesNode.Attributes["Name"].Value,
                            TemperatureDocument.NullDate,
                            TitleLineValueType.HourTick,
                            Convert.ToInt32(HeaderLinesNode.Attributes["Splits"].Value),
                            HeaderLinesNode.Attributes["ShowType"] == null ? "" : HeaderLinesNode.Attributes["ShowType"].Value,
                            ""));
                        break;
                }
            }

            //中间数值信息
            XmlNodeList YAxisInfosNodeList = configXml.SelectNodes("TemperatureDocument/YAxisInfos/YAxisInfo");
            Measure mea = new Measure();
            foreach (XmlNode YAxisInfosNode in YAxisInfosNodeList)
            {
                if (YAxisInfosNode.Attributes["Location"] !=null && YAxisInfosNode.Attributes["Location"].Value == "Left")
                {
                    var na = mea.GetType().GetProperty(YAxisInfosNode.Attributes["Id"].Value);
                    if (na != null)
                    {
                        //infoTitle.Add(TitlesNode.Attributes["Name"].Value + ":" + na.GetValue(document, null));
                        this.YAxisInfos.Add(new YAxisInfo(
                   YAxisInfosNode.Attributes["Name"].Value,
                   YAxisInfosNode.Attributes["Unit"].Value,
                            //ValueMemberType.Breath,
                   (ValueMemberType)Enum.Parse(typeof(ValueMemberType), YAxisInfosNode.Attributes["Id"].Value, true),
                   Convert.ToSingle(YAxisInfosNode.Attributes["Max"].Value),
                   Convert.ToSingle(YAxisInfosNode.Attributes["Min"].Value),
                            //PointSymbolStyle.HollowCicle,
                   (PointSymbolStyle)Enum.Parse(typeof(PointSymbolStyle), YAxisInfosNode.Attributes["PointSymbolStyle"].Value, true),
                   ColorTranslator.FromHtml(YAxisInfosNode.Attributes["Color"].Value),
                   Convert.ToInt32(YAxisInfosNode.Attributes["Types"].Value),
                   (YAxisInfosNode.Attributes["IsShowOnTemp"] == null ? 0 : (Convert.ToInt32(YAxisInfosNode.Attributes["IsShowOnTemp"].Value)))
                   ));
                    }
                }
            }

            // 页脚行
            XmlNodeList FooterMeasuresNodeList = configXml.SelectNodes("TemperatureDocument/FooterMeasures/FooterMeasure");
            FooterMeasure footer = new FooterMeasure();
            foreach (XmlNode FooterMeasuresNode in FooterMeasuresNodeList)
            {
                var na = footer.GetType().GetProperty(FooterMeasuresNode.Attributes["Id"].Value);
                if (na != null)
                {
                    this.FooterLines.Add(new TitleLineInfo(
                    FooterMeasuresNode.Attributes["Name"].Value,
                    TemperatureDocument.NullDate,
                    (TitleLineValueType)Enum.Parse(typeof(TitleLineValueType), FooterMeasuresNode.Attributes["Id"].Value, true),
                    Convert.ToInt32(FooterMeasuresNode.Attributes["Splits"].Value),
                    FooterMeasuresNode.Attributes["ShowType"] == null ? "" : FooterMeasuresNode.Attributes["ShowType"].Value,
                    FooterMeasuresNode.Attributes["MergeName"] == null ? "" : FooterMeasuresNode.Attributes["MergeName"].Value
                    ));
                }
            }
            //this.FooterLines.Add(new TitleLineInfo(
            //    "液入量ml",
            //    TemperatureDocument.NullDate,
            //    TitleLineValueType.Liquid_In));

            // 页脚备注
            XmlNodeList FooterRemarksNodeList = configXml.SelectNodes("TemperatureDocument/FooterRemarks/FooterRemark");
            FooterRemark footerRemark = new FooterRemark();
            foreach (XmlNode FooterRemarksNode in FooterRemarksNodeList)
            {
                if (FooterRemarksNode.Attributes["ShowType"]==null)
                {
                    this.FooterRemarks.Add(new FooterRemark(
                    FooterRemarksNode.Attributes["Name"].Value, FooterRemarksNode.Attributes["Text"].Value));
                }
                else
                {
                    //var show = FooterRemarksNode.Attributes["ShowType"] == null ? "" : FooterRemarksNode.Attributes["ShowType"].Value;  
                    this.FooterRemarks.Add(new FooterRemark(
                        FooterRemarksNode.Attributes["Name"].Value, FooterRemarksNode.Attributes["Text"].Value, FooterRemarksNode.Attributes["ShowType"].Value));
                }

            }

        }
    }
}
